
package com.yesep.learn.mqtt.server.broker.handler.protocol;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yesep.learn.mqtt.protocol.IPubComp;
import com.yesep.learn.mqtt.server.common.Constants;
import com.yesep.learn.mqtt.server.common.message.IDupPubRelMessageStoreService;
import com.yesep.learn.mqtt.server.common.message.IMessageIdService;
import com.yesep.learn.mqtt.util.ChannelUtil;

import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader;
import io.netty.util.AttributeKey;

/**
   * 消息编号：7
 * @Description: PUBCOMP连接处理，发布完成，保证传递3
 */
@Service
public class PubComp implements IPubComp {

	private static final Logger LOGGER = LoggerFactory.getLogger(PubComp.class);
	@Autowired
	private IMessageIdService messageIdService;
	@Autowired
	private IDupPubRelMessageStoreService dupPubRelMessageStoreService;

	public void process(Channel channel, MqttMessage msg) {
		MqttMessageIdVariableHeader variableHeader = (MqttMessageIdVariableHeader) msg.variableHeader();
		int messageId = variableHeader.messageId();
		String clientId = ChannelUtil.getClientId(channel);
		LOGGER.debug("PUBCOMP - clientId: {}, messageId: {}",clientId, messageId);
		dupPubRelMessageStoreService.remove(clientId, variableHeader.messageId());
		messageIdService.releaseMessageId(messageId);
	}
}
